//
//  CXBCarInfoViewModel.m
//  车信帮3.0
//
//  Created by liGordon on 16/7/18.
//  Copyright © 2016年 lifuli. All rights reserved.
//

#import "CXBCarInfoViewModel.h"

@implementation CXBCarInfoViewModel


+(instancetype)shareInfoModel
{
    static id instance;
    static dispatch_once_t once;
    dispatch_once(&once, ^{
        instance=[[self alloc] init];
    });
    return instance;
    
}

/**
 *  查询车辆品牌信息
 */

-(void)searchCarBrandInfoWithPID:(NSString *)pId andHierarchy:(NSString *)hierarchy Success:(void (^)(NSDictionary *result))success andfailure:(void (^)())failure
{
  
        UIDevice *device = [[UIDevice alloc]init];
        NSString *model = device.name;      //获取设备的类别
        NSString *systemVersion = device.systemVersion;//获取当前系统的版本
        NSString *currentVersion=[NSBundle mainBundle].infoDictionary[@"CFBundleShortVersionString"];
        NSString *token = [[NSUserDefaults standardUserDefaults]objectForKey:@"deviceToken"]?[[NSUserDefaults standardUserDefaults]objectForKey:@"deviceToken"]:@"";
    

    
    NSDictionary *obj = @{@"OpenCode":OPENCODE?OPENCODE:@"",
                          @"pId":pId,
                          @"hierarchy":hierarchy,
                          };
    NSString *objJson = [obj jsonString];
    

    
    NSString *dataStr = [XXTEA encryptStringToBase64String:objJson  stringKey:SelectKey sign:YES];
        NSDictionary *dict = @{
                               @"data": dataStr,
                               @"platform":@"2",//IOS
                               @"devicenumber":token,
                               @"devicename":model,
                               @"deviceversion":systemVersion,
                               @"version":currentVersion,
                               };
    
  [[CBXNetworkTool sharedNetworkTool] POST:[NSString stringWithFormat:@"http://%@/%@appkey=%@&timestamp=%@",BASEURL,@"api/Member/Address/GetMakeList?",appkey,[CBXNetworkTool timeString]] parameters:dict progress:^(NSProgress * _Nonnull uploadProgress) {
      
  } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
      
              //解密
              NSString *reslutStr = @"";
              reslutStr = [XXTEA decryptBase64StringToString:responseObject stringKey:SelectKey sign:YES];
              //        DLog(@"reslutStr=%@",reslutStr);
              NSDictionary *resultDic=[WHJSON objectFormJsonString:reslutStr];
              //        DLog(@"当前的字典是%@",resultDic);
              //成功之后回调
              success(resultDic);
  } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        failure();
  }];
    

    
    
}



/**
 *  添加用户车辆信息
 */
-(void)addUserCarInfoWithBuyTime:(NSString *)buyTime andCarModelId:(NSString *)carModelId andIsDefault:(NSString *)isDefault andMakeId:(NSString *)makeId andMakeName:(NSString *)makeName andMemberId:(NSString *)memberId andModelName:(NSString *)modelName andModelSeries:(NSString *)modelSeries andPlateNumber:(NSString *)PlateNumber andRemark:(NSString *)remark andTypeName:(NSString *)typeName andTypeSeries:(NSString *)typeSeries andVehicleClass:(NSString *)vehicleClass andCarColor:(NSString *)carColor andVehicleFrameNum:(NSString *)vehicleFrameNum andEngineNum:(NSString *)engineNum andTotalMileage :(NSString *)totalMileage andMakeLogo:(NSString *)makeLogo andDisplacement:(NSString *)displacement  andTypeId:(NSString *)TypeId Success:(void (^)(NSDictionary *result))success
{
    [MBProgressHUD showMessage:@"正在提交"];
//    UIDevice *device = [[UIDevice alloc]init];
//    NSString *model = device.name;      //获取设备的类别
//    NSString *systemVersion = device.systemVersion;//获取当前系统的版本
//    NSString *currentVersion=[NSBundle mainBundle].infoDictionary[@"CFBundleShortVersionString"];
//    NSString *token = [[NSUserDefaults standardUserDefaults]objectForKey:@"deviceToken"]?[[NSUserDefaults standardUserDefaults]objectForKey:@"deviceToken"]:@"";
    NSDictionary *obj = @{@"OpenCode":OPENCODE?OPENCODE:@"",
                          @"BuyTime":buyTime,
                          @"CarModelId":carModelId,
                          @"IsDefault":isDefault,
                          @"MakeId":makeId,
                          @"MakeName":makeName,
                          @"MemberId":memberId,
                          @"ModelName":modelName,
                          @"ModelSeries":modelSeries,
                          @"PlateNumber":PlateNumber,
                          @"Remark":remark,
                          @"TypeName":typeName,
                          @"TypeSeries":typeSeries,
                          @"VehicleClass":vehicleClass,
                          @"CarColor":carColor,
                          @"VehicleFrameNum":vehicleFrameNum,
                          @"EngineNum":engineNum,
                          @"TotalMileage":totalMileage,
                          @"MakeLogo":makeLogo,
                          @"Displacement":displacement,
                          @"TypeId":TypeId
                          };
    NSString *objJson = [obj jsonString];
    
    
    NSString *dataStr = [XXTEA encryptStringToBase64String:objJson  stringKey:SelectKey sign:YES];
    
    
    
//    NSDictionary *dict = @{
//                           @"data": dataStr,
//                           @"platform":@"2",//IOS
//                           @"devicenumber":token,
//                           @"devicename":model,
//                           @"deviceversion":systemVersion,
//                           @"version":currentVersion,
//                           };
    
    [[CBXNetworkTool sharedNetworkTool]POST:[NSString stringWithFormat:@"http://%@/%@appkey=%@&timestamp=%@",BASEURL,@"api/Member/Address/AddCarInfo?",appkey,[CBXNetworkTool timeString]] parameters:dataStr success:^(NSURLSessionDataTask * _Nonnull task, id  _Nonnull responseObject) {
          [MBProgressHUD hideHUD];
        [MBProgressHUD showSuccess:@"提交成功"];
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [MBProgressHUD hideHUD];
            
        });
        //解密
        NSString *reslutStr = @"";
        reslutStr = [XXTEA decryptBase64StringToString:responseObject stringKey:SelectKey sign:YES];
        //        DLog(@"reslutStr=%@",reslutStr);
        NSDictionary *resultDic=[WHJSON objectFormJsonString:reslutStr];
        //        DLog(@"当前的字典是%@",resultDic);
        //成功之后回调
        success(resultDic);
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        [MBProgressHUD hideHUD];
        [MBProgressHUD showError:strOfnoNet];
        
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            
            // 隐藏登录指示器
            [MBProgressHUD hideHUD];
        });
    }];
    
    
}




/**
 *  修改车辆信息
 */
-(void)changeUserCarInfoWithID:(NSString *)iD andBuyTime:(NSString *)buyTime andCarModelId:(NSString *)carModelId andIsDefault:(NSString *)isDefault andMakeId:(NSString *)makeId andMakeName:(NSString *)makeName andMemberId:(NSString *)memberId andModelName:(NSString *)modelName andModelSeries:(NSString *)modelSeries andPlateNumber:(NSString *)PlateNumber andRemark:(NSString *)remark andTypeName:(NSString *)typeName andTypeSeries:(NSString *)typeSeries andVehicleClass:(NSString *)vehicleClass andCarColor:(NSString *)carColor andVehicleFrameNum:(NSString *)vehicleFrameNum andEngineNum:(NSString *)engineNum andTotalMileage :(NSString *)totalMileage andMakeLogo:(NSString *)makeLogo andDisplacement:(NSString *)displacement  andTypeId:(NSString *)TypeId Success:(void (^)(NSDictionary *result))success
{


    
//    UIDevice *device = [[UIDevice alloc]init];
//    NSString *model = device.name;      //获取设备的类别
//    NSString *systemName = device.systemName;   //获取当前运行的系统
//    NSString *systemVersion = device.systemVersion;//获取当前系统的版本
//    NSString *currentVersion=[NSBundle mainBundle].infoDictionary[@"CFBundleShortVersionString"];
//    
//    NSString *token = [[NSUserDefaults standardUserDefaults]objectForKey:@"deviceToken"]?[[NSUserDefaults standardUserDefaults]objectForKey:@"deviceToken"]:@"";
    
    NSDictionary *obj = @{@"OpenCode":OPENCODE?OPENCODE:@"",
                          @"ID":iD,
                          @"BuyTime":buyTime,
                          @"CarModelId":carModelId,
                          @"IsDefault":isDefault,
                          @"MakeId":makeId,
                          @"MakeName":makeName,
                          @"MemberId":memberId,
                          @"ModelName":modelName,
                          @"ModelSeries":modelSeries,
                          @"PlateNumber":PlateNumber,
                          @"Remark":remark,
                          @"TypeName":typeName,
                          @"TypeSeries":typeSeries,
                          @"VehicleClass":vehicleClass,
                          @"CarColor":carColor,
@"TypeId":TypeId,
                          @"VehicleFrameNum":vehicleFrameNum,
                          @"EngineNum":engineNum,
                          @"TotalMileage":totalMileage,
                          @"MakeLogo":makeLogo,
                          @"Displacement":displacement,
                          };
    NSString *objJson = [obj jsonString];
    
    
    NSString *dataStr = [XXTEA encryptStringToBase64String:objJson  stringKey:SelectKey sign:YES];
    
    
    
//    NSDictionary *dict = @{
//                           @"data": dataStr,
//                           @"platform":@"2",//IOS
//                           @"devicenumber":token,
//                           @"devicename":model,
//                           @"deviceversion":systemVersion,
//                           @"version":currentVersion,
//                           };
    
    [[CBXNetworkTool sharedNetworkTool]POST:[NSString stringWithFormat:@"http://%@/%@appkey=%@&timestamp=%@",BASEURL,@"api/Member/Address/UpdateCarInfo?",appkey,[CBXNetworkTool timeString]] parameters:dataStr success:^(NSURLSessionDataTask * _Nonnull task, id  _Nonnull responseObject) {
        
        //解密
        NSString *reslutStr = @"";
        reslutStr = [XXTEA decryptBase64StringToString:responseObject stringKey:SelectKey sign:YES];
        //        DLog(@"reslutStr=%@",reslutStr);
        NSDictionary *resultDic=[WHJSON objectFormJsonString:reslutStr];
        //        DLog(@"当前的字典是%@",resultDic);
        //成功之后回调
        success(resultDic);
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        DLog(@"失败");
    }];
    
    
}



/**
 *  删除车辆信息
 */
-(void)deleteCarInfoWithCarInfoId:(NSString *)carInfoID  Success:(void (^)(NSDictionary *result))success
{
    
    NSDictionary *obj = @{@"OpenCode":OPENCODE?OPENCODE:@"",
                          @"carInfoId":carInfoID,
                          };
    NSString *objJson = [obj jsonString];
    
    
    NSString *dataStr = [XXTEA encryptStringToBase64String:objJson  stringKey:SelectKey sign:YES];
//    NSDictionary *dict = @{
//                           @"data": dataStr,
//                           @"platform":@"2",//IOS
//                           @"devicenumber":token,
//                           @"devicename":model,
//                           @"deviceversion":systemVersion,
//                           @"version":currentVersion,
//                           };
    
    [[CBXNetworkTool sharedNetworkTool]POST:[NSString stringWithFormat:@"http://%@/%@appkey=%@&timestamp=%@",BASEURL,@"api/Member/Address/DelCarInfo?",appkey,[CBXNetworkTool timeString]] parameters:dataStr success:^(NSURLSessionDataTask * _Nonnull task, id  _Nonnull responseObject) {
        
        //解密
        NSString *reslutStr = @"";
        reslutStr = [XXTEA decryptBase64StringToString:responseObject stringKey:SelectKey sign:YES];
        //        DLog(@"reslutStr=%@",reslutStr);
        NSDictionary *resultDic=[WHJSON objectFormJsonString:reslutStr];
        //        DLog(@"当前的字典是%@",resultDic);
        //成功之后回调  //101
        success(resultDic);
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {

    }];
    
    
}



/**
 *  查询单条或多条车辆信息
  flag: onw 单条    flag: list 列表
 */
-(void)searchSingleAndMultiCarInfoWithCarInfoId:(NSString *)carInfoID andFlag:(NSString *)flag Success:(void (^)(NSDictionary *result))success andFailer:(void (^)())failer
{
    
        UIDevice *device = [[UIDevice alloc]init];
        NSString *model = device.name;      //获取设备的类别
        NSString *systemName = device.systemName;   //获取当前运行的系统
        NSString *systemVersion = device.systemVersion;//获取当前系统的版本
        NSString *currentVersion=[NSBundle mainBundle].infoDictionary[@"CFBundleShortVersionString"];
    
        NSString *token = [[NSUserDefaults standardUserDefaults]objectForKey:@"deviceToken"]?[[NSUserDefaults standardUserDefaults]objectForKey:@"deviceToken"]:@"";

    
    NSDictionary *obj = @{@"OpenCode":OPENCODE?OPENCODE:@"",
                          @"carInfoId":carInfoID,
                          @"flag":flag,
                          };
    NSString *objJson = [obj jsonString];
    
    
    NSString *dataStr = [XXTEA encryptStringToBase64String:objJson  stringKey:SelectKey sign:YES];
    
    
//    
    NSDictionary *dict = @{
                           @"data": dataStr,
                           @"platform":@"2",//IOS
                           @"devicenumber":token,
                           @"devicename":model,
                           @"deviceversion":systemVersion,
                           @"version":currentVersion,
                           };
    
    [[CBXNetworkTool sharedNetworkTool]POST:[NSString stringWithFormat:@"http://%@/%@appkey=%@&timestamp=%@",BASEURL,@"api/Member/Address/GetCarInfoList?",appkey,[CBXNetworkTool timeString]] parameters:dict success:^(NSURLSessionDataTask * _Nonnull task, id  _Nonnull responseObject) {
        
        //解密
        NSString *reslutStr = @"";
        reslutStr = [XXTEA decryptBase64StringToString:responseObject stringKey:SelectKey sign:YES];
        //        DLog(@"reslutStr=%@",reslutStr);
        NSDictionary *resultDic=[WHJSON objectFormJsonString:reslutStr];
        //        DLog(@"当前的字典是%@",resultDic);
        //成功之后回调
        success(resultDic);
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        failer();
    }];
    
    
}
/**
 *  用户设置默认车辆
 */
-(void)setDefualtCarInfoWithCarInfoId:(NSString *)carInfoID  Success:(void (^)(NSDictionary *result))success
{

    
    NSDictionary *obj = @{@"OpenCode":OPENCODE?OPENCODE:@"",
                          @"carInfoId":carInfoID,
                          };
    NSString *objJson = [obj jsonString];
    
    
    NSString *dataStr = [XXTEA encryptStringToBase64String:objJson  stringKey:SelectKey sign:YES];
    
    [[CBXNetworkTool sharedNetworkTool]POST:[NSString stringWithFormat:@"http://%@/%@appkey=%@&timestamp=%@",BASEURL,@"api/Member/Address/SetDefautCarInfo?",appkey,[CBXNetworkTool timeString]] parameters:dataStr success:^(NSURLSessionDataTask * _Nonnull task, id  _Nonnull responseObject) {
        
        //解密
        NSString *reslutStr = @"";
        reslutStr = [XXTEA decryptBase64StringToString:responseObject stringKey:SelectKey sign:YES];

        NSDictionary *resultDic=[WHJSON objectFormJsonString:reslutStr];

        //成功之后回调
        success(resultDic);
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        DLog(@"失败");
    }];
    
    
}


/**
 *  根据车型id查询车型列表
 */
-(void)searchCarTypeListWithModelId:(NSString *)modelId andtypeSeries:(NSString *)typeSeries  Success:(void (^)(NSDictionary *result))success
{
    
    UIDevice *device = [[UIDevice alloc]init];
    NSString *model = device.name;      //获取设备的类别
    NSString *systemName = device.systemName;   //获取当前运行的系统
    NSString *systemVersion = device.systemVersion;//获取当前系统的版本
    NSString *currentVersion=[NSBundle mainBundle].infoDictionary[@"CFBundleShortVersionString"];
    
    NSString *token = [[NSUserDefaults standardUserDefaults]objectForKey:@"deviceToken"]?[[NSUserDefaults standardUserDefaults]objectForKey:@"deviceToken"]:@"";
    
    NSDictionary *obj = @{@"modelId":modelId,
                          @"typeSeries":typeSeries,
                          };
    NSString *objJson = [obj jsonString];
    
    
    NSString *dataStr = [XXTEA encryptStringToBase64String:objJson  stringKey:SelectKey sign:YES];
    NSDictionary *dict = @{
                           @"data": dataStr,
                           @"platform":@"2",//IOS
                           @"devicenumber":token,
                           @"devicename":model,
                           @"deviceversion":systemVersion,
                           @"version":currentVersion,
                           };
    [[CBXNetworkTool sharedNetworkTool]POST:[NSString stringWithFormat:@"http://%@/%@appkey=%@&timestamp=%@",BASEURL,@"api/Member/Address/GetCarModelList?",appkey,[CBXNetworkTool timeString]] parameters:dict success:^(NSURLSessionDataTask * _Nonnull task, id  _Nonnull responseObject) {
        //解密
        NSString *reslutStr = @"";
        reslutStr = [XXTEA decryptBase64StringToString:responseObject stringKey:SelectKey sign:YES];
        NSDictionary *resultDic=[WHJSON objectFormJsonString:reslutStr];
        //成功之后回调
        success(resultDic);
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        DLog(@"失败");
    }];
    
    
}


/**
 *  查询热门品牌列表
 */

-(void)searchHotCarBrandListSuccess:(void (^)(NSDictionary *result))success
{

    
    UIDevice *device = [[UIDevice alloc]init];
    NSString *model = device.name;      //获取设备的类别
    NSString *systemName = device.systemName;   //获取当前运行的系统
    NSString *systemVersion = device.systemVersion;//获取当前系统的版本
    NSString *currentVersion=[NSBundle mainBundle].infoDictionary[@"CFBundleShortVersionString"];
    
    NSString *token = [[NSUserDefaults standardUserDefaults]objectForKey:@"deviceToken"]?[[NSUserDefaults standardUserDefaults]objectForKey:@"deviceToken"]:@"";
    
    NSDictionary *obj = @{@"OpenCode":OPENCODE?OPENCODE:@"",
                          };
    NSString *objJson = [obj jsonString];
    
    
    NSString *dataStr = [XXTEA encryptStringToBase64String:objJson  stringKey:SelectKey sign:YES];
    
    
    
    NSDictionary *dict = @{
                           @"data": dataStr,
                           @"platform":@"2",//IOS
                           @"devicenumber":token,
                           @"devicename":model,
                           @"deviceversion":systemVersion,
                           @"version":currentVersion,
                           };
    
    [[CBXNetworkTool sharedNetworkTool]POST:[NSString stringWithFormat:@"http://%@/%@appkey=%@&timestamp=%@",BASEURL,@"api/Member/Address/GetHotMakeList?",appkey,[CBXNetworkTool timeString]] parameters:dict success:^(NSURLSessionDataTask * _Nonnull task, id  _Nonnull responseObject) {
        
        //解密
        NSString *reslutStr = @"";
        reslutStr = [XXTEA decryptBase64StringToString:responseObject stringKey:SelectKey sign:YES];
        //        DLog(@"reslutStr=%@",reslutStr);
        NSDictionary *resultDic=[WHJSON objectFormJsonString:reslutStr];
        //        DLog(@"当前的字典是%@",resultDic);
        //成功之后回调
        success(resultDic);
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        DLog(@"失败");
    }];
    
    
}

@end
